﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kunlun.Models;
using System.IO;
namespace Kunlun.BusinessLogic
{
    public partial class KunlunMembership
    {
        /// <summary>
        /// 客户相册根目录
        /// </summary>
        private string path = System.Configuration.ConfigurationManager.AppSettings["photopath"];

        KunLunEntities entities = new KunLunEntities();
        public Consumer GetUser(int userid)
        {
            Consumer consumer = entities.Consumers.Single(c => c.ID == userid);
            return consumer;

        }
        public bool  ChangePassword(Consumer user, string oldpassrord, string newpassword)
        {
            if (user.Password == oldpassrord)
            {
                user.Password = newpassword;                
                entities.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }

        }

        #region 注册用户
        /// <summary>
        /// 客户注册
        /// </summary>
        /// <param name="username">客户名称</param>
        /// <param name="password">客户密码</param>
        /// <param name="email">客户邮箱</param>
        /// <param name="regip">注册ip</param>
        /// <returns>返回注册客户的id</returns>
        public int Register(string username, string password, string email, string regip)
        {
            //添加客户信息
            Consumer reguser = new Consumer();
            reguser.UserName = username;
            reguser.Password = password;
            reguser.Email = email;
            reguser.RealName = string.Empty;
            reguser.Sex = 0;
            reguser.Phone = string.Empty;
            reguser.Status = 1;                     
            reguser.BirthDay = DateTime.Now;
            reguser.RegDate = DateTime.Now;
            reguser.RegIP = regip;
            reguser.Scoring = 0;
            reguser.Scored = 0;
            reguser.IsRss = true;
            reguser.Blacklist = false;
            reguser.LastUpdateTime = DateTime.Now;
            reguser.LeaveMoney = (decimal)0;
            entities.Consumers.Add(reguser);
            entities.SaveChanges();
            int userid = reguser.ID;

            //添加客户图片库
            Consumer_library lib = new Consumer_library();
            lib.Description = "默认的相册";
            lib.LibraryName = "默认的相册";
            lib.IsPub = true;
            lib.IsVisible = true;
            lib.LastUpdateTime = DateTime.Now;
            lib.UserID = userid;
            lib.Orders = 0;
            lib.BeginDate = DateTime.Now;
            entities.Consumer_library.Add(lib);
            entities.SaveChanges();

            string userpath = String.Format("{0}\\{1}-{2}\\{3}", path, DateTime.Now.Year, DateTime.Now.Month, userid);
            if (!Directory.Exists(userpath))
                Directory.CreateDirectory(userpath);

            return userid;
        }
        #endregion

        public bool CheckEmail(string email)
        {
            Consumer reguser = entities.Consumers.FirstOrDefault(u => u.Email == email);

            bool exit = (reguser == null);
            return exit;
        }
        public bool CheckName(string name)
        {
            Consumer reguser = entities.Consumers.FirstOrDefault(u => u.UserName == name);


            bool exit = (reguser == null);
            return exit;
        
        }
        public bool ChangEmail(Consumer user, string email)
        {
            if (user.Email.ToLower() != email.ToLower())
            {
                user.Email = email;
                entities.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }
    }


}
